//
//  HamatonCarSettingView.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/11.
//

import UIKit
import SnapKit

internal protocol HamatonCarSettingViewDelegate: NSObjectProtocol {
    
    /// 0开启，1关闭
    func hamatonCarSettingViewClickSwitch(tag :Int, type: Int)
    
    // type 1 姓名， Type 2手机号码
    func hamatonCarSettingViewTextFieldChange(text :String?, type: Int)
    
    // type 1所属网格 2 职位
    func hamatonCarSettingViewSelectedClick(type: Int)

    // 保存
    func hamatonCarSettingViewSaveClick()
    
    // 保存挂车数量
    func setTrailerNumFun()
    
    // 上下挂设置
    func goUpAndDownController(text: String)

}


class HamatonCarSettingView: UIView {
    internal weak var delegate: HamatonCarSettingViewDelegate?
    var deviceModel: DeviceSettingCellModel? {
        didSet {
            carTypeModel = deviceModel?.carTypeModel
            communicationTypeModel = deviceModel?.communicationTypeModel
            CANCommunicationBaudRateModel = deviceModel?.CANCommunicationBaudRateModel
            CANMailingAddressModel = deviceModel?.CANMailingAddressModel
            PCTRFChannel1Model = deviceModel?.PCTRFChannel1Model
            PCTRFChannel2Model = deviceModel?.PCTRFChannel2Model
            NumberOfTrailersModel = deviceModel?.NumberOfTrailersModel
            // String
            if let canterm: String = deviceModel?.CANTerminalResistanceModel {
                if canterm == "ON" {
                    CANResistanceSender.selectedSegmentIndex = 0
                } else {
                    CANResistanceSender.selectedSegmentIndex = 1
                }
            }
            if let temperature: String = deviceModel?.temperatureCompensationModel {
                if temperature == "ON" {
                    AlarmResetSender.selectedSegmentIndex = 0
                } else {
                    AlarmResetSender.selectedSegmentIndex = 1
                }
            }
        }
    }
    // 从选择页面返回
    // 车辆类型
    var carTypeModel: HamatonCarModel? {
        didSet {
            carTypeRightLabel.text = carTypeModel?.name ?? ""
            if carTypeModel?.id == "00" { // 头车
                NumberOfTrailersLabel.textColor = UIColor(hex6: 0x404040)
                NumberOfTrailersRightLabel.textColor = UIColor(hex6: 0x404040)
                NumberOfTrailersRightButton.isHidden = false
            } else {
                NumberOfTrailersLabel.textColor = UIColor(hex6: 0x999999)
                NumberOfTrailersRightLabel.textColor = UIColor(hex6: 0x999999)
                NumberOfTrailersRightButton.isHidden = true
            }
        }
    }
    // 通讯类型
    var communicationTypeModel: HamatonCarModel? {
        didSet {
            communicationTypeRightLabel.text = communicationTypeModel?.name ?? ""
            print(communicationTypeModel)
            if communicationTypeModel?.name.prefix(3) == "CAN" { // CAN类
                CANSpeedLabel.textColor = UIColor(hex6: 0x404040)
                CANSpeedRightLabel.textColor = UIColor(hex6: 0x404040)
                CANSpeedRightButton.isHidden = false
                
                CANResistanceLabel.textColor = UIColor(hex6: 0x404040)
                CANResistanceButton.isHidden = true
                
                CANAddressLabel.textColor = UIColor(hex6: 0x404040)
                CANAddressRightLabel.textColor = UIColor(hex6: 0x404040)
                CANAddressRightButton.isHidden = false
            } else {
                CANSpeedLabel.textColor = UIColor(hex6: 0x999999)
                CANSpeedRightLabel.textColor = UIColor(hex6: 0x999999)
                CANSpeedRightButton.isHidden = true
                
                CANResistanceLabel.textColor = UIColor(hex6: 0x999999)
                CANResistanceButton.isHidden = false
                
                CANAddressLabel.textColor = UIColor(hex6: 0x999999)
                CANAddressRightLabel.textColor = UIColor(hex6: 0x999999)
                CANAddressRightButton.isHidden = true
            }
        }
    }
    // CAN通讯波特率
    var CANCommunicationBaudRateModel: HamatonCarModel? {
        didSet {
            CANSpeedRightLabel.text = CANCommunicationBaudRateModel?.name ?? ""
        }
    }
    // CAN通讯地址
    var CANMailingAddressModel: HamatonCarModel? {
        didSet {
            CANAddressRightLabel.text = CANMailingAddressModel?.name ?? ""
        }
    }
    // RF通道1
    var PCTRFChannel1Model: HamatonCarModel? {
        didSet {
            RFChannel1RightLabel.text = PCTRFChannel1Model?.name ?? ""
        }
    }
    // RF通道2
    var PCTRFChannel2Model: HamatonCarModel? {
        didSet {
            RFChannel2RightLabel.text = PCTRFChannel2Model?.name ?? ""
        }
    }
    // 挂车数量
    var NumberOfTrailersModel: HamatonCarModel? {
        didSet {
            NumberOfTrailersRightLabel.text = NumberOfTrailersModel?.name ?? "0"
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    // 车辆类型
    private lazy var carTypeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "车辆类型"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    
    lazy var carTypeRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    
    private lazy var carTypeRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    
    lazy var carTypeRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // 通讯类型
    private lazy var communicationTypeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "通讯类型"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    lazy var communicationTypeRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    private lazy var communicationTypeRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var communicationTypeRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // CAN通讯波特率
    private lazy var CANSpeedLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "CAN通讯波特率"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    lazy var CANSpeedRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    private lazy var CANSpeedRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var CANSpeedRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    private lazy var lineView3: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // CAN终端电阻
    private lazy var CANResistanceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "CAN终端电阻"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()

    private lazy var CANResistanceSender: UISegmentedControl = {
        let segmentedControl = UISegmentedControl(items: ["ON", "OFF"])
        // 选项颜色
        segmentedControl.tintColor = UIColor.blue
        // 默认选中第二项
        segmentedControl.selectedSegmentIndex = 0
        // 点击事件
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
        // 2
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
        // 3
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: UIControl.State.selected)
        // 4
        segmentedControl.backgroundColor = .white
        addSubview(segmentedControl)
        return segmentedControl
    }()

    private lazy var CANResistanceButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var lineView4: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // CAN通讯地址
    private lazy var CANAddressLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "CAN通讯地址"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    lazy var CANAddressRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    private lazy var CANAddressRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var CANAddressRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    private lazy var lineView5: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // RF通道1
    private lazy var RFChannel1Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "RF通道1"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    lazy var RFChannel1RightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    private lazy var RFChannel1RightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var RFChannel1RightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    private lazy var lineView6: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // RF通道2
    private lazy var RFChannel2Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "RF通道2"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    lazy var RFChannel2RightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    private lazy var RFChannel2RightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var RFChannel2RightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    private lazy var lineView7: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // 温度补偿
    private lazy var AlarmResetLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "温度补偿"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()

    private lazy var AlarmResetSender: UISegmentedControl = {
        let segmentedControl = UISegmentedControl(items: ["ON", "OFF"])
        // 选项颜色
        segmentedControl.tintColor = UIColor.blue
        // 默认选中第二项
        segmentedControl.selectedSegmentIndex = 0
        // 点击事件
        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
        // 2
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
        // 3
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: UIControl.State.selected)
        // 4
        segmentedControl.backgroundColor = .white
        addSubview(segmentedControl)
        return segmentedControl
    }()

    private lazy var lineView8: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
    // 挂车数量
    private lazy var NumberOfTrailersLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "挂车数量"
        label.backgroundColor = .clear
        label.textAlignment = .left
        addSubview(label)
        return label
    }()
    lazy var NumberOfTrailersRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        addSubview(view)
        return view
    }()
    private lazy var NumberOfTrailersRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    private lazy var NumberOfTrailersRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "0"
        label.backgroundColor = .clear
        label.textAlignment = .right
        addSubview(label)
        return label
    }()
    private lazy var lineView9: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        addSubview(view)
        return view
    }()
//    // 上下挂设置
//    private lazy var UpAndDownLabel: UILabel = {
//        let label = UILabel()
//        label.textColor = UIColor(hex6:0x404040)
//        label.font = Theme.Font.size(16)
//        label.text = "上下挂设置"
//        label.backgroundColor = .clear
//        label.textAlignment = .left
//        addSubview(label)
//        return label
//    }()
//
//    private lazy var UpAndDownSender: UISegmentedControl = {
//        let segmentedControl = UISegmentedControl(items: ["ON", "OFF"])
//        // 选项颜色
//        segmentedControl.tintColor = UIColor.blue
//        // 默认选中第二项
//        segmentedControl.selectedSegmentIndex = 0
//        // 点击事件
//        segmentedControl.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
//        // 2
//        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: UIControl.State.normal)
//        // 3
//        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue], for: UIControl.State.selected)
//        // 4
//        segmentedControl.backgroundColor = .white
//        addSubview(segmentedControl)
//        return segmentedControl
//    }()
//
//    private lazy var lineView9: UIView = {
//        let view = UIView()
//        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
//        addSubview(view)
//        return view
//    }()
    
    lazy var saveBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("保存", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickSaveButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        addSubview(button)
        return button
    }()
//    lazy var refreshBtn: UIButton = {
//        let button = UIButton(type: .custom)
//        button.setTitle(NSLocalizedString("刷新", tableName: "HamatonLocalizable", comment: ""), for: .normal)
//        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
//        button.addTarget(self, action: #selector(onClickRefreshButton), for: .touchUpInside)
//        button.backgroundColor  = Theme.Color.Background.maintwo
//        button.layer.masksToBounds = true
//        button.layer.cornerRadius = 5
//        addSubview(button)
//        return button
//    }()
    lazy var upAndDownBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("上下挂设置", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.addTarget(self, action: #selector(onClickUpAndDownButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.mainthree
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        addSubview(button)
        return button
    }()
    
    private func setupUI() {
        backgroundColor = .clear
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 10
        // Do any additional setup after loading the view.
        // 车辆类型
        carTypeLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        
        carTypeRightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(carTypeLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }

        carTypeRightLabel.snp.makeConstraints { make in
            make.right.equalTo(carTypeRightImg.snp.left).offset(margRight)
            make.centerY.equalTo(carTypeLabel.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(14)
        }

        lineView1.snp.makeConstraints { make in
            make.top.equalTo(carTypeLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        carTypeRightButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView1.snp.top).offset(0)
        }
        // 通讯类型
        communicationTypeLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        communicationTypeRightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(communicationTypeLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        communicationTypeRightLabel.snp.makeConstraints { make in
            make.right.equalTo(communicationTypeRightImg.snp.left).offset(margRight)
            make.centerY.equalTo(communicationTypeLabel.snp.centerY)
            make.width.equalTo(160)
            make.height.equalTo(14)
        }

        lineView2.snp.makeConstraints { make in
            make.top.equalTo(communicationTypeLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        communicationTypeRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView2.snp.top).offset(0)
        }
        // CAN通讯波特率
        CANSpeedLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        CANSpeedRightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(CANSpeedLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        CANSpeedRightLabel.snp.makeConstraints { make in
            make.right.equalTo(CANSpeedRightImg.snp.left).offset(margRight)
            make.centerY.equalTo(CANSpeedLabel.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(14)
        }
        lineView3.snp.makeConstraints { make in
            make.top.equalTo(CANSpeedLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        CANSpeedRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView3.snp.top).offset(0)
        }
        // CAN终端电阻
        CANResistanceLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }

        CANResistanceSender.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(30)
            make.centerY.equalTo(CANResistanceLabel.snp.centerY)
        }

        lineView4.snp.makeConstraints { make in
            make.top.equalTo(CANResistanceLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        CANResistanceButton.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView4.snp.top).offset(0)
        }
        // CAN通讯地址
        CANAddressLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        CANAddressRightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(CANAddressLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        CANAddressRightLabel.snp.makeConstraints { make in
            make.right.equalTo(CANAddressRightImg.snp.left).offset(margRight)
            make.centerY.equalTo(CANAddressLabel.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(14)
        }
        lineView5.snp.makeConstraints { make in
            make.top.equalTo(CANAddressLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        CANAddressRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView5.snp.top).offset(0)
        }
        // RF通道1
        RFChannel1Label.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        RFChannel1RightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(RFChannel1Label.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        RFChannel1RightLabel.snp.makeConstraints { make in
            make.right.equalTo(RFChannel1RightImg.snp.left).offset(margRight)
            make.centerY.equalTo(RFChannel1Label.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(14)
        }
        lineView6.snp.makeConstraints { make in
            make.top.equalTo(RFChannel1Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        RFChannel1RightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView6.snp.top).offset(0)
        }
        // RF通道2
        RFChannel2Label.snp.makeConstraints { make in
            make.top.equalTo(lineView6.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        RFChannel2RightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(RFChannel2Label.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        RFChannel2RightLabel.snp.makeConstraints { make in
            make.right.equalTo(RFChannel2RightImg.snp.left).offset(margRight)
            make.centerY.equalTo(RFChannel2Label.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(14)
        }
        lineView7.snp.makeConstraints { make in
            make.top.equalTo(RFChannel2Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        RFChannel2RightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView6.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView7.snp.top).offset(0)
        }
        // 温度补偿
        AlarmResetLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView7.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }

        AlarmResetSender.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(30)
            make.centerY.equalTo(AlarmResetLabel.snp.centerY)
        }

        lineView8.snp.makeConstraints { make in
            make.top.equalTo(AlarmResetLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 挂车数量
        NumberOfTrailersLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView8.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.right.equalToSuperview().offset(margRight)
            make.height.equalTo(20)
        }
        NumberOfTrailersRightImg.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(margRight)
            make.centerY.equalTo(NumberOfTrailersLabel.snp.centerY)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        NumberOfTrailersRightLabel.snp.makeConstraints { make in
            make.right.equalTo(RFChannel2RightImg.snp.left).offset(margRight)
            make.centerY.equalTo(NumberOfTrailersLabel.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(14)
        }
        lineView9.snp.makeConstraints { make in
            make.top.equalTo(NumberOfTrailersLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        NumberOfTrailersRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView8.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView9.snp.top).offset(0)
        }
        // 上下挂设置
        upAndDownBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-200)
        }
        // 保存
        saveBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-140)
        }
        // 刷新
//        refreshBtn.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(26)
//            make.right.equalToSuperview().offset(-26)
//            make.height.equalTo(40)
//            make.bottom.equalToSuperview().offset(-80)
//        }
    }
    
    
    @objc func segmentedControlChange(_ segmented: UISegmentedControl) {
        if segmented == CANResistanceSender { // 终端电阻
            print("CAN终端电阻")
            delegate?.hamatonCarSettingViewClickSwitch(tag: segmented.selectedSegmentIndex, type: 1)
        } else if segmented == AlarmResetSender { // 温度补偿
            delegate?.hamatonCarSettingViewClickSwitch(tag: segmented.selectedSegmentIndex, type: 2)
        }
      //  delegate?.hamatonCarSettingViewClickGender(tag: segmented.selectedSegmentIndex)
    }
    
//    @objc func nameTextFieldDidChange(tf: UITextField) {
//        if tf == nameTextField {
//           // delegate?.hamatonCarSettingViewTextFieldChange(text: tf.text, type: 1)
//        }else {
//           // delegate?.hamatonCarSettingViewTextFieldChange(text: tf.text, type: 2)
//        }
//    }
//
    // 去选择
    @objc func onClickButton(_ sender: UIButton) {
        if sender == carTypeRightButton {
            print("去选择车辆类型")
            delegate?.hamatonCarSettingViewSelectedClick(type: 1)
        } else if sender == communicationTypeRightButton {
            print("去选择通讯类型")
            delegate?.hamatonCarSettingViewSelectedClick(type: 2)
        } else if sender == CANSpeedRightButton {
            print("去选择CAN通讯波特率")
            delegate?.hamatonCarSettingViewSelectedClick(type: 3)
        } else if sender == CANAddressRightButton {
            print("去选择CAN通讯地址")
            delegate?.hamatonCarSettingViewSelectedClick(type: 4)
        } else if sender == RFChannel1RightButton {
            print("去选择RF通道1")
            delegate?.hamatonCarSettingViewSelectedClick(type: 5)
        } else if sender == RFChannel2RightButton {
            print("去选择RF通道2")
            delegate?.hamatonCarSettingViewSelectedClick(type: 6)
        } else if sender == NumberOfTrailersRightButton {
            print("去选择挂车数量")
            delegate?.hamatonCarSettingViewSelectedClick(type: 7)
        }
    }
    
    // 保存
    @objc func onClickSaveButton() {
        delegate?.hamatonCarSettingViewSaveClick()
//        delegate?.setTrailerNumFun()
    }
    
    // 上下挂设置
    @objc func onClickUpAndDownButton() {
        delegate?.goUpAndDownController(text: NumberOfTrailersRightLabel.text!)
    }
    
    // 刷新
    @objc func onClickRefreshButton() {
        print("刷新")
    }

}
